﻿namespace game.display.hud
{

    public class HUDLives : lumo.display.object2D.IObject2D, lumo.display.object2D.IPositionable2D
    {

        protected Microsoft.Xna.Framework.Graphics.Texture2D _Texture;
        protected System.Collections.Generic.List<lumo.display.object2D.Sprite> _Sprites;
        protected uint _MaxLives;

        protected const float _SPACEMENT = 8f;

        public uint Lives
        {
            get { return (uint)_Sprites.Count; }
            set
            {
                value = value > _MaxLives ? _MaxLives : value;
                if (value == _Sprites.Count)
                    return;
                _Sprites.Clear();
                for (int i = 0; i < value; i++)
                    _Sprites.Add(new lumo.display.object2D.Sprite(_Texture));
                Position = Position;
                Visible = Visible;
            }
        }

        public uint MaxLives
        {
            get { return _MaxLives; }
            set
            {
                _MaxLives = value;
                Lives = Lives;
            }
        }

        public HUDLives(Microsoft.Xna.Framework.Graphics.Texture2D texture)
        {
            _Texture = texture;
            if (_Texture == null)
                throw new System.Exception("You can't set this texture to null");
            _Sprites = new System.Collections.Generic.List<lumo.display.object2D.Sprite>();
        }

        #region IObject2D

        public void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch)
        {
            for (int i = 0; i < _Sprites.Count; i++)
                _Sprites[i].Draw(spriteBatch);
        }

        protected float _DrawOrder = 0f;

        public float DrawOrder
        {
            get { return _DrawOrder; }
            set { _DrawOrder = value; }
        }

        protected bool _Visible = true;

        public bool Visible
        {
            get { return _Visible; }
            set
            {
                _Visible = value;
                for (int i = 0; i < _Sprites.Count; i++)
                    _Sprites[i].Visible = value;
            }
        }

        #endregion

        #region IPositionable2D

        protected Microsoft.Xna.Framework.Vector2 _Position;

        public Microsoft.Xna.Framework.Vector2 Position
        {
            get { return _Position; }
            set
            {
                _Position = value;
                if (_Sprites.Count == 0)
                    return;
                _Sprites[0].Position = value;
                for (int i = 1; i < _Sprites.Count; i++)
                    _Sprites[i].Position = value + new Microsoft.Xna.Framework.Vector2(i * (_Texture.Width + _SPACEMENT), 0);
            }
        }

        public float X
        {
            get { return Position.X; }
            set { Position = new Microsoft.Xna.Framework.Vector2(value, Y); }
        }

        public float Y
        {
            get { return Position.Y; }
            set { Position = new Microsoft.Xna.Framework.Vector2(X, value); }
        }

        #endregion

    }

}